<template>
	<view class="login">

		<button class='cs_button userInfo' type='primary' open-type="getUserInfo" lang="zh_CN" @getuserinfo="bindGetUserInfo">
			授权登录
		</button>

		<pop ref="popup">
			<view class="slot u-f-ajc">
				<button @tap="reject">拒绝</button>
				<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号</button>
			</view>
		</pop>
	</view>
</template>

<script>
	import pop from "@/components/uni-popup/uni-popup.vue";
	export default {
		data() {
			return {
				canIUse: wx.canIUse('button.open-type.getUserInfo')
			}
		},
		components: {
			pop
		},
		methods: {
			open() {
				// 需要在 popup 组件，指定 ref 为 popup
				this.$refs.popup.open()
			},
			getPhoneNumber: function(e) {
				this.getIphoneAndLogin(e)

			},
			getIphoneAndLogin(e) {
				function login(e){
					// api 获取用戶手機號
					that.$http.request({
						url: 'https://后台解密地址',
						data: {
							'encryptedData': encodeURIComponent(e.detail.encryptedData),
							'iv': e.detail.iv,
							'code': res.code
						},
						method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
						header: {
							'content-type': 'application/json'
						}, // 设置请求的 header
						success: function(res) {
							//存入缓存即可
							that.userInfo.iphone = res.phone
					
						},
						fail: function(err) {
							console.log(err);
						},
						complete() {
							user.login(that.userInfo, true)
						}
					})
				}
				let that = this;
				console.log(e)
				uni.login({
					success: res => {
						
					},
					complete() {
						
					}
				})
				uni.switchTab({
					url: 'index/index'
				})
			},
			reject() {
				uni.switchTab({
					url: 'index/index'
				})
			},
			bindGetUserInfo: function(e) {
				if (e.detail.userInfo) {
					uni.setStorage({
						key:'userInfo',
						data: e.detail.userInfo
					})
					this.open();
				} else {
					//用户按了拒绝按钮
					wx.showModal({
						title: '警告',
						content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
						showCancel: false,
						confirmText: '返回授权',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击了“返回授权”')
							}
						}
					})
				}
			}
		}
	}
</script>

<style>
	.login{
		
	}
</style>
